﻿using Farming.Domain;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Farming.EntityFrameworkCore
{
    public class FarmingDbContext : DbContext
    {

        public FarmingDbContext(DbContextOptions<FarmingDbContext> options) : base(options) { }
        public DbSet<Admin> Admins { get; set; }
        public DbSet<Goods> Goods { get; set; }
        public DbSet<GoodsType> GoodsTypes { get; set; }
        public DbSet<LogMessage> LogMessage { get; set; }

        //Fluent Api

        //protected override void OnModelCreating(ModelBuilder modelBuilder)
        //{
        //    modelBuilder.Entity<Admin>(t =>
        //    {
        //        t.HasKey(t => t.AdminId);
        //        t.Property(t => t.UserName).HasMaxLength(50).IsRequired();
        //        t.Property(t => t.Password).HasMaxLength(50).IsRequired();
        //        t.Property(t => t.LastLoginIp).HasMaxLength(50);
        //    });
        //    //数据播种
        //    modelBuilder.Entity<Admin>().HasData(new Admin
        //    {
        //        UserName = "Admin",
        //        Password = Guid.NewGuid().ToString("n"),
        //        AdminId = 1,
        //        CreateById = 1,
        //        CreateByName = "admin",
        //        CreateTime = DateTime.Now,
        //        IsDeleted = false
        //    },
        //    new Admin
        //    {
        //        UserName = "zhangsan",
        //        Password = Guid.NewGuid().ToString("n"),
        //        AdminId = 2,
        //        CreateById = 2,
        //        CreateByName = "zhangsan",
        //        CreateTime = DateTime.Now.AddDays(1),
        //        IsDeleted = false
        //    }
        //    );
        //    base.OnModelCreating(modelBuilder);
        //}
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // apply global query filter
            modelBuilder.Entity<Admin>().HasQueryFilter(x => x.IsDeleted == false);
            base.OnModelCreating(modelBuilder);
        }
        //public override int SaveChanges()
        //{
        //    SetSystemField();
        //    return base.SaveChanges();
        //}
        //public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken))
        //{
        //    SetSystemField();
        //    return base.SaveChangesAsync();
        //}
        /// <summary>
        /// 系统字段赋值
        /// </summary>
        //    private void SetSystemField()
        //    {
        //        foreach (var item in ChangeTracker.Entries())
        //        {
        //            if (item.Entity is AuditFields)
        //            {
        //                var entity = (AuditFields)item.Entity;
        //                //添加操作
        //                if (item.State == EntityState.Added)
        //                {
        //                    entity.CreaterName = "admin";
        //                    entity.CreateTime = DateTime.Now;
        //                    entity.IsDeleted = false;
        //                }
        //                //修改操作
        //                else if (item.State == EntityState.Modified)
        //                {
        //                    entity.DeleterName = "admin";
        //                    entity.DeleteTime = DateTime.Now;
        //                    entity.LastModifierName = "admin";
        //                    entity.LastModifyTime = DateTime.Now;
        //                }
        //            }

        //        }
        //    }

        //}
        public class SoftDeleteEntity : ISoftDeleteEntityWithDeleted
        {
            public int Id { get; set; }
            public string Name { get; set; } = "test";
            public bool IsDeleted { get; set; }
        }
    }
}
